package com.woniuxy.education.mapper;

import com.woniuxy.education.entity.Class;
import com.woniuxy.education.entity.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface StuAccountManageMapper {
    //创建账户
    @Insert("insert into user (account,password,type_id) values (#{account},#{password},#{type_id})")
    public void addUser(User user);
    //通过uid查询用户
    @Select("select * from user where id=#{u_id}")
    public User queryUserByU_id(Integer u_id);
    //重置密码
    @Update("update user set password='123456' where id=#{u_id}")
    public void updateUserPassword(Integer u_id);
    //添加学生
    @Insert("insert into student (number,name,c_id,u_id) values(#{number},#{name},#{c_id},#{u_id})")
    public void addStudent(Student student);
    //通过学号查询学生
    @Select("select * from student where number=#{number}")
    public Student queryStudentByNumber(Integer number);
    @Select("select * from student where id=#{id}")
    public Student queryStudentById(Integer id);
    //通过账号查询用户
    @Select("select * from user where account=#{account}")
    public User queryUserByAccount(String account);
    //查询班级内学生
    @Select("select * from student where c_id=#{c_id}")
    public List<Student> findStudent(Integer c_id);
    //模糊查询学生
    @Select("select * from student where c_id=#{c_id} and name like  CONCAT('%',#{name},'%')")
    public List<Student> searchStu(@Param("c_id") Integer c_id,@Param("name") String name);
    //添加学生转班信息
    @Insert("insert into change_stuclass(s_id,nc_id,ac_id,application_date,state,pett_id,conft_id,reason) value(#{student.id},#{class1.id},#{class2.id},#{application_date},#{state},#{petteacher.id},#{confteacher.id},#{reason})")
    public void addChangeStuClass(Change_StuClass change_StuClass);
   //查询班级
    @Select("select * from class where id=#{c_id}")
    public Class searchClassByC_id(Integer c_id);
    //获取所有班级信息
    @Select("select * from class")
    public List<Class> getAllClass();
    //查询转班信息
    @Select("select * from change_stuclass where state=#{state} and nc_id=#{c_id} or ac_id=#{c_id}")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "s_id",property = "student",
            many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.queryStudentById")),
            @Result(column = "nc_id",property="class1",
            many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.searchClassByC_id")),
            @Result(column = "ac_id",property="class2",
                    many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.searchClassByC_id")),
            @Result(column = "application_date", property = "application_date"),
            @Result(column = "state",property = "state"),
            @Result(column = "pett_id",property = "petteacher",
            many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.queryTeacherById")),
            @Result(column = "conft_id",property = "confteacher",
                    many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.queryTeacherById")),
            @Result(column = "reason",property = "reason")
    })
    public List<Change_StuClass> queryStuChangeClass(@Param("c_id") Integer c_id,@Param("state") Integer state);
    //查询教师
    @Select("select * from teacher where name=#{name}")
    public Teacher queryTeacherByName(String name);
    //查询教师
    @Select("select * from teacher where id=#{id}")
    public Teacher queryTeacherById(Integer id);

    @Select("select * from change_stuclass where s_id=#{s_id} and state=0")
    public Change_StuClass queryStuChangClassByS_id(Integer s_id);
    //通过id查询单个转班消息
    @Select("select * from change_stuclass where id=#{id}")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "s_id",property = "student",
                    many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.queryStudentById")),
            @Result(column = "nc_id",property="class1",
                    many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.searchClassByC_id")),
            @Result(column = "ac_id",property="class2",
                    many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.searchClassByC_id")),
            @Result(column = "application_date", property = "application_date"),
            @Result(column = "state",property = "state"),
            @Result(column = "pett_id",property = "petteacher",
                    many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.queryTeacherById")),
            @Result(column = "conft_id",property = "confteacher",
                    many = @Many(select = "com.woniuxy.education.mapper.StuAccountManageMapper.queryTeacherById")),
            @Result(column = "reason",property = "reason")
    })
    public Change_StuClass queryStuChangClassById(Integer id);
    //跟新消息
    @Update("update change_stuclass set state=#{state},end_date=#{end_date} where id=#{id}")
    public boolean updateStuChangClassById(@Param("state") Integer state,@Param("id") Integer id,@Param("end_date") String end_date);
    //修改学生关联班级
    @Update("update student set c_id=#{c_id} where id=#{id}")
    public boolean updateStudentClass(@Param("id") Integer id,@Param("c_id") Integer c_id);
    @Select("select * from class where id in (select c_id from grade_class where g_id=#{g_id})")
    public List<Class> getClassByG_id(Integer g_id);
}
